package cn.ctvonline.android.kaopu.activity.demo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.base.BaseAppCompatActivity;
import cn.ctvonline.android.kaopu.base.util.ToastUtil;

/**
 * Created by dujian on 15/12/25.
 */
public class ActivityPullRefershDemo extends BaseAppCompatActivity {
    //    @Bind(R.id.pull_rv)
//    PullToRefershRecycleView pullRv;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pullrefersh_demo);
        initViews();
    }

    private void initViews() {
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            list.add("这是第" + i + " 条数据");
        }
        adapter = new PullRefershAdapter(getApplicationContext(), list);
//        pullRv.setRvAdapter(adapter);
    }

    @Override
    protected String generatePageName() {
        return "pageName is ActivityPullRefershDemo";
    }

    class PullRefershAdapter extends RecyclerView.Adapter {
        private List<String> list;
        private LayoutInflater inflater;

        public PullRefershAdapter(Context context, List<String> list) {
            inflater = LayoutInflater.from(context);
            this.list = list;
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.pullrefersh_item, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            String intro = list.get(position);
            vh.introTv.setText(intro);
            vh.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.show(getApplicationContext(), "item click");
                }
            });
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            View view;
            @Bind(R.id.pull_refersh_item_intro_tv)
            TextView introTv;

            public ViewHolder(View view) {
                super(view);
                this.view = view;
                ButterKnife.bind(this, view);
            }
        }
    }
}
